package Solver;

import java.util.LinkedList;

import maze.Maze;
import maze.Tile;

public class Solver {

	static int mazeID = 0;
	static LinkedList<Node> states = new LinkedList<Node>();

	public static void solve(Maze maze) throws CloneNotSupportedException {
		LinkedList<Maze> q = new LinkedList<Maze>();
		LinkedList<Maze> v = new LinkedList<Maze>();
		maze.setId(mazeID++);
		q.addLast(maze);
		while (!q.isEmpty()) {
			Maze m = q.removeFirst();
			v.addLast(m); // fakes search 3al visited
			LinkedList<Maze> mazes = new LinkedList<Maze>();
			System.out.println("Original Maze:");
			m.display();
			// System.out.println(m.getList().toString());
			System.out.println();
			for (int i = 0; i < m.getList().size(); i++) {
				Tile t = m.getList().get(i);
				// System.out.println("check Tile " + t.getID());
				if (t.getOrientation().equals("H")) {
					// move left or right
					if (m.canMove(t.getID(), "Left")) {
						// System.out.println("Can move left");
						Maze r = m.copy();
						r.move(t.getID(), "Left");
						boolean visited = isVisted(v, r);
						if (!visited) {
							// System.out.println("Add");
							r.setId(mazeID++);
							mazes.addLast(r);
							// v.addLast(r);
							Maze tmp = r.copy();
							q.addLast(tmp);
						}
						/*
						 * System.out.println("Original Maze:"); m.display();
						 * System.out.println(m.getList().toString());
						 * System.out.println();
						 * System.out.println("After movement");
						 * mazes.getLast().display();
						 */
					} else if (m.canMove(t.getID(), "Right")) {
						// System.out.println("can move right");
						Maze r = m.copy();
						r.move(t.getID(), "Right");
						boolean visited = isVisted(v, r);
						if (!visited) {
							// System.out.println("Add");
							r.setId(mazeID++);
							mazes.addLast(r);
							// v.addLast(r);
							Maze tmp = r.copy();
							q.addLast(tmp);
						}
						/*
						 * System.out.println("Original Maze:"); m.display();
						 * System.out.println(m.getList().toString());
						 * System.out.println();
						 * System.out.println("After movement");
						 * mazes.getLast().display();
						 */
					}
				} else if (t.getOrientation().equals("V")) {
					// move up or down
					if (m.canMove(t.getID(), "Up")) {
						// System.out.println("Can move Up");
						Maze r = m.copy();
						r.move(t.getID(), "Up");
						boolean visited = isVisted(v, r);
						if (!visited) {
							// System.out.println("Add");
							r.setId(mazeID++);
							mazes.addLast(r);
							// v.addLast(r);
							Maze tmp = r.copy();
							q.addLast(tmp);
						}
						/*
						 * System.out.println("Original Maze:"); m.display();
						 * System.out.println(m.getList().toString());
						 * System.out.println();
						 * System.out.println("After movement");
						 * mazes.getLast().display();
						 */
					} else if (m.canMove(t.getID(), "Down")) {
						// System.out.println("can move Down");
						Maze r = m.copy();
						r.move(t.getID(), "Down");
						boolean visited = isVisted(v, r);
						if (!visited) {
							// System.out.println("Add");
							r.setId(mazeID++);
							mazes.addLast(r);
							// v.addLast(r);
							Maze tmp = r.copy();
							q.addLast(tmp);
						}
						/*
						 * System.out.println("Original Maze:"); m.display();
						 * System.out.println(m.getList().toString());
						 * System.out.println();
						 * System.out.println("After movement");
						 * mazes.getLast().display();
						 */
					}
				}
			}
			System.out.println("Next States:");
			for (int i = 0; i < mazes.size(); i++) {
				// System.out.println("Maze " + mazes.get(i).getId());
				mazes.get(i).display();
				System.out.println();
			}
	//		states.add(new Node(m.getId(), mazes));
		}
//		displayStates();
	}

	/*private static void displayStates() {
		System.out.println("--------States--------");
		for (int i = 0; i < states.size(); i++) {
			System.out.println("State: " + states.get(i).getId());
			//LinkedList<Maze> adj = states.get(i).getAdjList();
			for (int j = 0; j < adj.size(); j++) {
				adj.get(j).display();
			}
			System.out.println();
		}
	}*/

	private static boolean isVisted(LinkedList<Maze> v, Maze r) {
		for (int i = 0; i < v.size(); i++) {
			if (v.get(i).equals(r))
				return true;
		}
		return false;
	}

	public static void main(String[] args) throws CloneNotSupportedException {
		Maze m = new Maze();
		solve(m);
	}

	}
